package com.oocl.consumer;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.oocl.dao.impl.BookDaoImpl;
import com.oocl.dao.pojo.Book;
import com.oocl.json2book.Json2Book;

public class BookDataConsumer {
	
	public void bookDataConsumer(){
		ConnectionFactory factory = new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
		Destination queue = new ActiveMQQueue("Lionel");
		Connection con;
		MessageConsumer mc;
		Session sen ;
		Json2Book j2b=new Json2Book();
		BookDaoImpl bdi=new BookDaoImpl();
		Book book;
		while (true) {
			try {
				con = factory.createConnection();
				con.start();
				sen = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
				mc = sen.createConsumer(queue);
				TextMessage msg = (TextMessage) mc.receive();
				System.out.println(msg.getText());
				book=j2b.json2Book(msg.getText());
//				bdi.addBook(book);
				mc.close();
				sen.close();
				con.close();
			} catch (JMSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}

			
	}
	public static void main(String[] args) {
		BookDataConsumer bdc=new BookDataConsumer();
		bdc.bookDataConsumer();
	}

}
